package com.dayouzi.esaydata.controller;


import com.dayouzi.esaydata.base.result.R;
import com.dayouzi.esaydata.base.result.ResponseEnum;
import com.dayouzi.esaydata.pojo.page.PageBean;
import com.dayouzi.esaydata.pojo.query.UserInfoQuery;
import com.dayouzi.esaydata.pojo.vo.UserInfoDTO;
import com.dayouzi.esaydata.pojo.vo.UserInfoVO;
import com.dayouzi.esaydata.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DaYouZi
 * @since 2023-03-06
 */

@Api(description = "用户信息管理")
@Slf4j
@RestController
@RequestMapping("/flac/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 分页查询用户信息list
     * @param page
     * @param limit
     * @param userInfoQuery
     * @return
     */
    @ApiOperation("分页查询用户信息list")
    @PostMapping("/listPage/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody UserInfoQuery userInfoQuery
            ) {
        PageBean<UserInfoVO> result = userInfoService.listPage(page, limit, userInfoQuery);
        return R.ok().data("result", result);
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("/getInfoById/{id}")
    public R getInfoById(
            @ApiParam(value = "用户id", required = true) @PathVariable Integer id
    ) {
        UserInfoVO result = userInfoService.getInfoById(id);
        return R.ok().data("result", result);
    }

    /**
     * 根据用户id删除用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据用户id删除用户信息")
    @GetMapping("/removeById/{id}")
    public R removeById(
            @ApiParam(value = "用户id", required = true) @PathVariable Integer id
    ) {
        Boolean flag = userInfoService.removeInfoById(id);
        if (!flag) {
            return R.setResult(ResponseEnum.DELETE_FAILED);
        }
        return R.ok().code(200).message("删除成功");
    }

    /**
     * 根据用户id恢复用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据用户id恢复用户信息")
    @GetMapping("/resumeById/{id}")
    public R resumeById(
            @ApiParam(value = "用户id", required = true) @PathVariable Integer id
    ) {
        Boolean flag = userInfoService.resumeById(id);
        if (!flag) {
            return R.setResult(ResponseEnum.RESUME_FAILED);
        }
        return R.ok().code(200).message("恢复成功");
    }

    /**
     * 根据用户id永久删除用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据用户id永久删除用户信息")
    @GetMapping("/foreverRemove/{id}")
    public R foreverRemove(
            @ApiParam(value = "用户id", required = true) @PathVariable Integer id
    ) {
        Boolean flag = userInfoService.foreverRemove(id);
        if (!flag) {
            return R.setResult(ResponseEnum.DELETE_FAILED);
        }
        return R.ok().code(200).message("删除成功");
    }

    /**
     * 根据用户id修改用户信息
     * @param userInfoDTO
     * @return
     */
    @ApiOperation("根据用户id修改用户信息")
    @PostMapping("/updateById")
    public R updateById(
            @ApiParam(value = "用户信息对象", required = true) @Valid @RequestBody UserInfoDTO userInfoDTO
    ) {
        Boolean flag = userInfoService.updateInfoById(userInfoDTO);
        if (!flag) {
            return R.error().code(-213).message("修改失败");
        }
        return R.ok().code(200).message("修改成功");
    }

    /**
     * 新增用户信息
     * @param userInfoDTO
     * @return
     */
    @ApiOperation("新增用户信息")
    @PostMapping("/insertUserInfo")
    public R insertUserInfo(
            @ApiParam(value = "用户信息对象", required = true) @Valid @RequestBody UserInfoDTO userInfoDTO
    ) {
        Boolean flag = userInfoService.insertUserInfo(userInfoDTO);
        if (!flag) {
            return R.error().code(-213).message("新增失败");
        }
        return R.ok().code(200).message("新增成功");
    }

    @ApiOperation("根据用户账号生成随机md5密码")
    @GetMapping("/initPassword")
    public R initPassword(
            @ApiParam(value = "用户账号", required = true) @RequestParam String userAccount
    ) {
        return R.ok().data("result", userInfoService.initPassword(userAccount));
    }

    @ApiOperation("初始化饼图数据")
    @GetMapping("/getPieData")
    public R getPieData() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "abc");
        map.put("value", 100);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "ABC");
        map1.put("value", 200);
        list.add(map);
        list.add(map1);
        return R.ok().data("result", list);
    }

}

